﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace DARE
{
    /// <summary>
    /// Represents a widget that only shows text.
    /// </summary>
    [AttrTypeDependentSerializable(typeof(CStaticTextData))]
    public class CStaticText : CWidget
    {
        #region fields

        private bool m_centered = false;

        private string m_text;
        private string m_displayed;
        private SpriteFont m_spriteFont;
        private Vector2 m_textPos;
        private float m_scale;

        /// <summary>
        /// Gets/Sets the text contained in this widget
        /// </summary>
        public string Text { get { return m_text; } set { m_text = value; m_displayed = getDisplayedText(); } }
        public SpriteFont Font { get { return m_spriteFont; } set { m_spriteFont = value; } }
        public float Scale { get { return m_scale; } set { m_scale = value; } }
        /// <summary>
        /// Gets/Sets the centered status.
        /// If this value is set to true, the text will be centered
        /// </summary>
        public bool IsCentered { get { return m_centered; } set { m_centered = value; } }

        #endregion

        #region displaycompute

        /// <summary>
        /// Gets the string which will be displayed.
        /// </summary>
        public string getDisplayedText()
        {
            for (int i = 0; i < m_text.Length; ++i)
                if (m_spriteFont.MeasureString(m_text.Substring(0, i)).X > m_rect.Width)
                {
                    if (i > 0)
                        return m_text.Substring(0, i - 1);
                    return "";
                }
            return m_text;
        }

        #endregion

        #region ctor

        public CStaticText(CHUDSystem hud, CWidget parent, string name)
            : base(hud, parent, name)
        {
            m_text = "";
            m_spriteFont = HUD.Skinner.GetSpriteFont("statictextdefault");
            m_textPos = Vector2.Zero;
            m_scale = 1;
            m_displayed = getDisplayedText(); ;
            Size = new Vector2(200, 50);
        }

        #endregion

        #region actualize

        /// <summary>
        /// Actualizes the widget.
        /// If anything has changed, it will allow this widget to get back its parameters.
        /// For example, if the size of the widget is a percentage of the size of its parent,
        /// if the parent's size changes, it will recalculate the good size of this widget.
        /// (This method is called automatically but you can force a new actualization).
        /// </summary>
        public override void Actualize()
        {
            base.Actualize();
            if (m_centered)
            {
                Vector2 v = m_spriteFont.MeasureString(m_displayed);
                m_textPos = new Vector2(m_rect.X + (m_rect.Width / 2.0f) - (v.X / 2.0f), m_rect.Y + (m_rect.Height / 2.0f) - (v.Y / 2.0f));
            }
            else
                m_textPos = new Vector2(m_rect.X, m_rect.Y);
            m_displayed = getDisplayedText();
        }

        /// <summary>
        /// Gets the max width of this widget regarding the size of the string
        /// </summary>
        /// <returns></returns>
        public override float getMaxWidth()
        {
            return m_spriteFont.MeasureString(m_text).X;
        }

        #endregion

        #region draw

        public override bool Draw(GameTime gameTime)
        {
            if (!m_isVisible)
                return false;
            if (m_textPos != new Vector2(m_rect.X, m_rect.Y) && !m_centered)
                m_textPos = new Vector2(m_rect.X, m_rect.Y);
            else if (m_textPos != new Vector2(m_rect.X, m_rect.Y))
            {
                Vector2 v = m_spriteFont.MeasureString(m_displayed);
                m_textPos = new Vector2(m_rect.X + (m_rect.Width / 2.0f) - (v.X / 2.0f), m_rect.Y + (m_rect.Height / 2.0f) - (v.Y / 2.0f));
            }
            base.Draw(gameTime);
            Color old = HUD.Shader.HighLightColor;
            bool wasHL = HUD.Shader.IsHighLighted;
            if (IsHighLighted)
            {
                HUD.Shader.IsHighLighted = IsHighLighted;
                HUD.Shader.HighLightColor = HLColor;
            }
            SpriteBatch.DrawString(m_spriteFont, m_displayed, m_textPos, Color, 0.0f, Vector2.Zero, m_scale, SpriteEffects.None, 0);
            if (IsHighLighted)
                HUD.Shader.HighLightColor = old;
            HUD.Shader.IsHighLighted = wasHL;
            return true;
        }

        #endregion

    }
}
